package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.seckill.SeckillFeign;
import com.atguigu.gmall.feign.user.UserFeign;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.order.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/9/18 1:05
 */
@Controller
public class SecKillController {
    @Autowired
    private SeckillFeign seckillFeign;
    @Autowired
    private UserFeign userFeign;
    @GetMapping("/seckill.html")
    public String index(Model model) {

        Result<List<SeckillGoods>> listResult = seckillFeign.seckillList();
        if (listResult.isOk()) {
            model.addAttribute("list", listResult.getData());
        }

        return "seckill/index";
    }

    //seckill/46.html
    @GetMapping("/seckill/{skuId}.html")
    public String detail(Model model, @PathVariable Long skuId) {
        Result<SeckillGoods> seckillGoodsResult = seckillFeign.seckillDetail(skuId);
        if (seckillGoodsResult.isOk()) {
            model.addAttribute("item", seckillGoodsResult.getData());
        }
        return "seckill/item";
    }
    ///seckill/queue.html?skuId=39&skuIdStr=a9895b17f2b06b2b9c518324a19cad16
    @GetMapping("/seckill/queue.html")
    public String queue(Model model, @RequestParam("skuId") Long skuId,@RequestParam("skuIdStr") String skuIdStr) {
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", skuIdStr);
        return "seckill/queue";
    }
    ///seckill/trade.html
    @GetMapping("/seckill/trade.html/{skuId}")
    public String trade(Model model, @PathVariable("skuId") Long skuId) {
        //detailArrayList
        //detail.imgUrl
        //detail.skuName
        //detail.orderPrice
        //detail.skuNum
        //totalNum
        //totalAmount
        //userAddressList
        Result<OrderInfo> result = seckillFeign.orderDetails(skuId);
        if (result.isOk()) {
            OrderInfo orderInfo = result.getData();
            model.addAttribute("detailArrayList", orderInfo.getOrderDetailList());
            model.addAttribute("totalNum","1");
            model.addAttribute("totalAmount",orderInfo.getTotalAmount());
            model.addAttribute("userAddressList",userFeign.userAddress());
        }

        return "seckill/trade";
    }

}
